﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Xml;
using bmapi.HelperClass;
using bmapi.Models.CADModel;
using bmapi.Models.DataEntity;

namespace bmapi.Controllers
{
    [RoutePrefix("cadLogin")]
    public class CADLoginController : CADController
    {
        [HttpPost, Route("Login")]
        public string Login(dynamic json)
        {
            WriteLog("Login", "json:"+json.ToString(), true);
            if (json == null)
                return ReturnError("没有登录信息");
            else
            {
                WriteDebug("Login", "2");
                string loginid = json.login_id.ToString();
                string password = json.password.ToString();
                string computerName = json.compute.ToString();

                SQLCommand sc = new SQLCommand("select * from User_tb where login_id=@loginid");
                sc.Add("@loginid", loginid);
                DataRow dr = sc.ExecuteOneRow();
                if(dr == null)
                    return ReturnError("系统中没有当前登录信息");
                User loginuser = AssistFun.DataRowToObject<User>(dr);
                WriteDebug("Login", "3");
                if (loginuser.password == password.Trim())
                {
                    //生成token
                    WriteDebug("Login", "4");
                    string token = CreateToken(loginuser.login_id);
                    //改变登录时间以及记录电脑名
                    UpdateLoginTimeAndComputer(loginuser.login_id,computerName);
                    WriteDebug("Login", "成功登录,当前登陆工号:"+loginuser.login_id);
                    //统计表
                    if (loginuser.power!=2 && IsFourteenDays())
                        Stats(loginuser.PLID);

                    return ReturnXml("成功", token, loginuser);
                }
                else
                    return ReturnError("输入密码有误");
            }
        }
        /// <summary>
        /// 成功登陆后返回cad的xml数据
        /// </summary> 
        /// <param name="user"></param>
        /// <param name="tip"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        private string ReturnXml(string tip, string token, User user)
        {
            WriteDebug("ReturnXml", "进入返回xml");
            XmlDocument doc = new XmlDocument();
            XmlNode root = doc.CreateElement("login");
            doc.AppendChild(root);

            XmlElement p = doc.CreateElement("status");
            p.InnerText = "200";
            root.AppendChild(p);

            p = doc.CreateElement("tip");
            p.InnerText = tip;
            root.AppendChild(p);

            p = doc.CreateElement("token");
            p.InnerText = token;
            root.AppendChild(p);

            p = doc.CreateElement("name");
            p.InnerText = user.name;
            root.AppendChild(p);

            p = doc.CreateElement("process");
            p.InnerText = user.code.ToString();
            root.AppendChild(p);

            //这个人,是否需要在结束任务时, 上传样式文件
            //SQLCommand sc = new SQLCommand("select upload from Process_tb where code=@code and PLID=@plid");
            //sc.Add("@code", user.code);  一人多工序  在CAD 那么upload取哪个呢  或者说这个upload 的值是在什么时候设置的
            SQLCommand sc = new SQLCommand("select upload from Process_tb where code in(" + user.code + "," + user.code2 + "," + user.code3 + ") and PLID=@plid");
            sc.Add("@plid", user.PLID);
            string upload = sc.ExecuteScalar();
            WriteDebug("ReturnXml", "upload:" + upload);

            p = doc.CreateElement("upload");
            p.InnerText = upload;
            root.AppendChild(p);

            p = doc.CreateElement("root");
            p.InnerText = m_buyiRoot;
            root.AppendChild(p);

            sc = new SQLCommand("SELECT key_val FROM System_tb where key_name=@name", "@name", "FtpPassword");
            string keyval = sc.ExecuteScalar();
            p = doc.CreateElement("passwod");
            if (keyval != null)
                p.InnerText = keyval;
            else
                p.InnerText = "By2018";
            root.AppendChild(p);
            WriteDebug("ReturnXml", "keyval:" + keyval);
            return ConvertXmlToString(doc);
        }

        private string ReturnError(string tip)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode root = doc.CreateElement("login");
            doc.AppendChild(root);

            XmlElement p = doc.CreateElement("status");
            p.InnerText = "201";
            root.AppendChild(p);

            p = doc.CreateElement("tip");
            p.InnerText = tip;
            root.AppendChild(p);

            return ConvertXmlToString(doc);
        }
    }
}
